<?php
namespace app\admin\controller;

use app\common\model\Express as ExpressModel;
use app\common\model\ExpressShipping as ShippingModel;
/**
 * 物流控制器
 */
class Express extends Admin
{
	/**
     * 物流公司列表
     */
    public function index(){
        $name = $this->request->param('name/s');
        $status = $this->request->param('status/d');

        $where = [];
        $map = [];
        $query = [];
        if(!empty($name)){
            $where[] = ['name','like','%'.$name.'%'];
            $query['name'] = $name;
        }

        if(!empty($status)){
            $map['status'] = $status;
            $query['status'] = $status;
        }

        $list = ExpressModel::where($map)->where($where)->order('id desc')->paginate(15, false, ['query' => $query]);

        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 添加物流公司
     */
    public function add(){
        if($this->request->isPost()){
            $data = $this->request->post();
            //验证
            $result = $this->validate($data,[
                'name|物流公司名称' => 'require|unique:express',
                'code|物流公司代码' => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($res = ExpressModel::create($data)){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }else{
            $this->assign('info', null);

            return $this->fetch('edit');
        }
    }

    /**
     * 编辑物流公司
     */
    public function edit($id = 0){
        if($this->request->isPost()){
             $data = $this->request->post();
            //验证
            $result = $this->validate($data,[
                'name|物流公司名称' => 'require|unique:express',
                'code|物流公司代码' => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if($res = ExpressModel::update($data)){
                $this->success('编辑成功');
            }else{
                $this->error('编辑失败');
            }
        }else{
            empty($id) && $this->error('参数错误');

            $info = ExpressModel::where('id',$id)->find();
            $this->assign('info', $info);

            return $this->fetch();
        }
    }

    /**
     * 删除物流公司
     * @param  integer $id 
     * @return mixed
     */
    public function del($id = 0){
        empty($id) && $this->error('参数错误');

        if(is_array($id)){
            $map[] = ['id','in',$id];
        }elseif(is_numeric($id)){
            $map[] = ['id','=',$id];
        }

        if($query = ExpressModel::where($map)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 更新物流公司状态
     * @param  integer $id 
     * @return mixed
     */
    public function update(){
        $data = $this->request->param();

        if(empty($data)){
            $this->error('参数错误');
        }   
        
        if($query = ExpressModel::update($data)){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }

    /**
     * 物流模板
     */
    public function template(){
    	$map = [];

    	$list = ShippingModel::where($map)->order('id desc')->paginate();

    	$this->assign('list', $list);
    	return $this->fetch();
    }

    /**
     * 新增物流模板
     */
    public function addTemplate(){
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		//验证
            $result = $this->validate($data,[
                'name|模板名称' => 'require',
                'company_id|物流公司' => 'require',
                'type|计费方式' => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);

            if(!empty($data['rule'])){
                $rule = array();
                for($i = 0; $i < count($data['rule']['region']);$i++){
                    $item['region'] = $data['rule']['region'][$i];
                    $item['first'] = $data['rule']['first'][$i];
                    $item['first_fee'] = $data['rule']['first_fee'][$i];
                    $item['next'] = $data['rule']['next'][$i];
                    $item['next_fee'] = $data['rule']['next_fee'][$i];
                    $rule[] = $item;
                }
                
                $data['rule'] = json_encode($rule);
            }

            if($res = ShippingModel::create($data)){
            	$this->success('添加成功');
            }else{
            	$this->error('添加失败');
            }
    	}else{
    		$express = ExpressModel::where('status=1')->select();
    		$this->assign('express', $express);  //物流公司
    		$this->assign('info', null);

    		return $this->fetch('editTemplate');
    	}
    }

    /**
     * 编辑物流公司模板
     */
    public function editTemplate($id = 0){
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		//验证
            $result = $this->validate($data,[
                'name|模板名称' => 'require',
                'company_id|物流公司' => 'require',
                'type|计费方式' => 'require'
            ]);
            // 验证失败 输出错误信息
            if(true !== $result) $this->error($result);            

            if(!empty($data['rule'])){
                $rule = array();
                for($i = 0; $i < count($data['rule']['region']);$i++){
                    $item['region'] = $data['rule']['region'][$i];
                    $item['first'] = $data['rule']['first'][$i];
                    $item['first_fee'] = $data['rule']['first_fee'][$i];
                    $item['next'] = $data['rule']['next'][$i];
                    $item['next_fee'] = $data['rule']['next_fee'][$i];
                    $rule[] = $item;
                }
                
                $data['rule'] = json_encode($rule);
            }         

            if($res = ShippingModel::update($data)){
            	$this->success('编辑成功');
            }else{
            	$this->error('编辑失败');
            }
    	}else{
    		empty($id) && $this->error('参数错误');

    		$info = ShippingModel::where('id',$id)->find();
    		$express = ExpressModel::where('status=1')->select();

            $info['rule'] = json_decode($info['rule'], true);

    		$this->assign('express', $express);  //物流公司
    		$this->assign('info', $info);

    		return $this->fetch('editTemplate');
    	}
    }

    /**
     * 删除物流公司模板
     */
    public function delTemplate($id = 0){
    	empty($id) && $this->error('参数错误');

        if(is_array($id)){
            $map[] = ['id','in',$id];
        }elseif(is_numeric($id)){
            $map[] = ['id','=',$id];
        }

        if($query = ShippingModel::where($map)->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 更新物流公司模板
     * @param  integer $id 
     * @return mixed
     */
    public function updateTemplate(){
        $data = $this->request->param();

        if(empty($data)){
            $this->error('参数错误');
        }   
        
        if($query = ShippingModel::update($data)){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }
 
    /**
     * 区域选择
     */
    public function region(){
        return $this->fetch();
    }
}